import { HttpContext } from '@angular/common/http';
import { Component, OnInit } from '@angular/core';
import { ALLOW_ANONYMOUS } from '@delon/auth';
import { SFSchema, SFUISchema, SFSelectWidgetSchema, SFRadioWidgetSchema } from '@delon/form';
import { _HttpClient } from '@delon/theme';
import { NzMessageService } from 'ng-zorro-antd/message';
import { NzModalRef } from 'ng-zorro-antd/modal';
import { Api } from 'src/app/shared/api/api';
import { USER_LABEL_ENUM, DELETED_STATUS_ENUM, VCATEGORIES, XS_SOURCE, SIX } from 'src/app/shared/constant/common-constant';
import { ResponseCode } from 'src/app/shared/response.code';

@Component({
  selector: 'app-system-add-record-contract',
  templateUrl: './user-add-record.component.html'
})
export class UserAddRecordComponent implements OnInit {
  record: any = {};
  readonly: boolean = false;
  userLabel: any = [];
  userStatus: any = [];
  xs_source_list: any = [];
  six_list: any = [];
  schema: SFSchema = { properties: {} };
  ui: SFUISchema = {
    '*': {
      spanLabel: 10,
      spanControl: 14,
      grid: { span: 12 }
    }
  };
  postData: any = {};

  constructor(private modal: NzModalRef, private msgSrv: NzMessageService, public http: _HttpClient) {}

  ngOnInit(): void {
    this.readonly = this.record.id && this.record.id.length > 0;
    this.userLabel = USER_LABEL_ENUM.filter(item => item.value.length > 0);
    this.userStatus = VCATEGORIES.filter(item => item.value !== null);
    this.xs_source_list = XS_SOURCE.filter(item => item.value !== null);
    this.six_list = SIX.filter(item => item.value !== null);
  }

  // 初始化表单，在获取完部门树数据之后再初始化
  initSchema() {
    this.schema = {
      properties: {
        id: {
          type: 'string',
          title: '客户id',
          readOnly: true
        }
      }
    };
  }

  save(value: any) {
    console.log(value);
    this.postData.id = this.record.id;
    this.postData.user_id = localStorage.getItem('user_id');
    this.postData.user_name = localStorage.getItem('user_name');
    this.postData.enable = true;
    this.http.post(Api.CustomerRecordAddApi, value, {}, { context: new HttpContext().set(ALLOW_ANONYMOUS, true) }).subscribe((res: any) => {
      if (res) {
        if (res.code === ResponseCode.SUCCESS) {
          this.msgSrv.success('添加成功');
          this.record.last_flow_up_record = value.content;
          this.updateMyUser(this.record);
          this.modal.close(true);
        } else {
          this.msgSrv.warning(res.message);
        }
      } else {
        this.msgSrv.error('添加失败，未知错误');
      }
    });
  }
  close() {
    this.modal.destroy();
  }
  updateMyUser(record: any) {
    this.http
      .post(
        Api.CustomerUpdateRecordAddTimeApi,
        [record],
        {},
        {
          headers: {
            Authorization: 'Bearer dGVzdDp0ZXN0'
          },
          context: new HttpContext().set(ALLOW_ANONYMOUS, true)
        }
      )
      .subscribe((res: any) => {
        if (res && res.code === ResponseCode.SUCCESS) {
          this.msgSrv.success('更新成功');
        }
      });
  }
}
